package luanvan.app.foodnote;


import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import luanvan.app.foodnote.library.GooglePlaces;
import luanvan.app.foodnote.library.JSONParser;
import luanvan.app.foodnote.library.Place;
import luanvan.app.foodnote.library.PlacesList;


import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;


public class LocationActivity extends SherlockFragmentActivity  {

    
	PlacesList nearPlaces;
	GooglePlaces googlePlaces;
	private GoogleMap mMap1;
	
	SupportMapFragment supportMapFragment;
	// Map overlay items
	
	LatLng toPosition;
	//AddItemizedOverlay itemizedOverlay;
	
	ProgressDialog pDialog;
	
	Double latitude;
	Double longitude;
	
	// JSON parser class
    JSONParser jsonParser = new JSONParser();
	
	//private static final String url_food_detials = "http://10.0.2.2/foodnote/get_food_details.php";
	
    private static final String TAG_LATITUDE = "LATITUDE";
    private static final String TAG_LONGITUDE = "LONGITUDE";
    
    double lat, lng;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.form_map);
        
        getSupportActionBar().setIcon(getResources().getDrawable(R.drawable.showmap));
        getSupportActionBar().show();

        Intent i = getIntent();
        lat = i.getExtras().getDouble(TAG_LATITUDE);
        lng = i.getExtras().getDouble(TAG_LONGITUDE);
        Log.d("Location Extra", String.valueOf(lat) + ", " + String.valueOf(lng));
        if(lat != 0.0 && lng != 0.0){
	        supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapview);
	        
	        mMap1 = supportMapFragment.getMap();
	        
	        LatLng coordinates = new LatLng(lat, lng);
	        mMap1.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 14)); 
	        mMap1.setMyLocationEnabled(true);
	        
	        mMap1.addCircle(new CircleOptions().center(new LatLng(lat, lng)).radius(1000).strokeColor(Color.RED)
	        		.strokeWidth(3));
	        mMap1.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title("Vị trí món ăn"));
		        
	        
	        //mMap1.addMarker(new MarkerOptions().position(coordinates).title("Vị trí món ăn"));
		        
	        //mMap1.addCircle(new Ci);
	        
	       new LoadPlaces().execute(lat, lng);
        }
	}
	
	private class LoadPlaces extends AsyncTask<Double, String, String>{

		double lat2, lng2;
		@Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(LocationActivity.this);
            pDialog.setTitle("Xin chờ");
            pDialog.setMessage("Đang tải các địa điểm ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
	 	
		@Override
		protected String doInBackground(Double... args) {
			lat2 = args[0];
			lng2 = args[1];
			googlePlaces = new GooglePlaces();
			
			try {
				String types = "cafe|food|restaurant"; // Listing places only cafes, restaurants, foods restaurant|
				 
                // Radius in meters - increase this value if you don't find any places
                double radius = 1000;
				nearPlaces = googlePlaces.search(lat2, lng2, radius, types);
				
			} catch(Exception e){
				e.printStackTrace();
			}
			
			//PlacesList places = null;
			
			return null;
		}
		protected void onPostExecute(String file_url) {
	        // dismiss the dialog after getting all products
	        pDialog.dismiss();
	        
	        runOnUiThread(new Runnable() {
				
				public void run() {
					if(nearPlaces.results != null){
				        
			        	for(Place place : nearPlaces.results){
			        		latitude = place.geometry.location.lat;
			        		longitude = place.geometry.location.lng;
			        		
			        		toPosition = new LatLng(latitude, longitude);
			        		
			        		mMap1.addMarker(new MarkerOptions().position(toPosition).title(place.name)
			        				.snippet(place.vicinity).icon(BitmapDescriptorFactory.fromResource(R.drawable.mark_blue)));
			        		
			        	}
					
					} // End if nearPlaces.results
				}
			});
		}
	}
	

}
